package com.le.tester.leetcode;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.lang.StringEscapeUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * createTime：2023/2/23 16:31
 * description：通过投票对团队排名
 */
public class RankTeams {
    private static final ObjectMapper MAPPER = new ObjectMapper();

    public String rankTeams(String[] votes) {

        Map<Character, int[]> map = new HashMap<>();

        for (String vote : votes) {
            int length = vote.length();
            for (int i = 0; i < length; i++) {
                //有key的话在原来的基础上修改，没有key的话直接进行新增
                int[] rankArr = map.getOrDefault(vote.charAt(i), new int[26]);
                rankArr[i]++;
                map.put(vote.charAt(i), rankArr);

            }
        }

        List<Map.Entry<Character, int[]>> rankList = new ArrayList<>(map.entrySet());
        rankList.sort((item1, item2) -> {
            int[] rank1 = item1.getValue();
            int[] rank2 = item2.getValue();
            for (int i = 0; i < 26; i++) {
                if (rank1[i] != rank2[i]) {
                    return rank2[i] - rank1[i];
                }
            }
            return item1.getKey() - item2.getKey();
        });

        return rankList.stream().map(entry -> String.valueOf(entry.getKey())).collect(Collectors.joining());
    }

    //map根据val排序
    public List<Character> sortMapByVal(Map<Character, Integer> map) {

        List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet());
        return list.stream()
                .sorted(Comparator.comparing(Map.Entry<Character, Integer>::getValue).reversed())
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
    }

    public static void main(String[] args) {
        String temp = "1,1,1";
        String[] split = temp.split(",");
        System.out.println(split.length);

    }


}
